public class ServerListener extends Thread{
@Override
public void run() {
try {
@SuppressWarnings("resource")
ServerSocket serverSocket = new ServerSocket(12345);
while(true) {
// accept() 會阻塞,等待用戶加入
Socket socket = serverSocket.accept();
// 創一個 ChatSocket 物件,用於讀取、寫入
ChatSocket cs = new ChatSocket(socket);
// ChatManager 的 SingleTon 管理這些 Socket
ChatManager.getChatManager().add(cs);
//run
cs.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}
public class ChatSocket extends Thread{
Socket socket ;
public ChatSocket(Socket s) {
this.socket = s;
// 對自己推送訊息
out("**已成功連線,歡迎加入聊天室**");
}
@Override
public void run() {
BufferedReader br;
try {
br = new BufferedReader(new InputStreamReader(socket.getInputStream(),"utf-8"));
String line = null;
while(socket.isConnected()){
line = br.readLine();
// null 和 心跳包符號 不推送
// 其餘都推
if(line != null && !line.equals("$")) {
// 對陣列所有 Socket 用戶推送訊息
ChatManager.getChatManager().publish(line);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
socket = null;
} catch (Exception e2) {
// TODO: handle exception
}
}
}
// 推送
public void out(String out) {
out = out + "\n";
try {
// 寫的對象為自己的 Socket
socket.getOutputStream().write(out.getBytes("UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class ChatManager {
ArrayList<String> arrayList = new ArrayList<>();
private ChatManager() {}
private static final ChatManager cm = new ChatManager();
public static ChatManager getChatManager() {
return cm;
}
// Vector 存所有 ChatSocket
Vector<ChatSocket> vector = new Vector<ChatSocket>();
public void add(ChatSocket cs) {
vector.add(cs);
}
public void publish(String out) {
for(int i = 0 ; i < vector.size() ; i++) {
// 藉由每個 ChatSocket 取得 Socket 對象
ChatSocket csChatSocket = vector.get(i);
if(!csChatSocket.socket.isClosed() && csChatSocket.socket.isConnected()){
// 對每個用戶 Socket 將訊息推送出去
csChatSocket.out(out);
}else {
vector.get(i).socket = null;
vector.remove(i);
i = i--;
}
}
}
}